/*
 * conexiones.h
 *
 *  Created on: 22/11/2011
 *      Author: utn_so
 */

#ifndef CONEXIONES_H_
#define CONEXIONES_H_

#define READ 	'1'
#define WRITE 	'2'
#define VECTORCOMIENZAENCERO 1
#define PISARULTIMOSECTORINCOMPLETO 1
#define rootclus 2

#include <time.h>
#include "bootsecto.h"
#include "lista.h"
#include "nipc.h"

typedef enum {
	FREE = 1,
	BUSY = 2,
}status;

struct t_conexiones{
		int		socket;
		status 	libre;
}typedef t_conexion;

typedef struct
{
	unsigned int 	sector;
	char			*buffer;
}sec_arch;

int available_connection();

int connect_Raid_PPD();

void armarPayload(nipcMessage msg, unsigned int sector, char *buffer);

struct t_cache *check_cache(struct TDD_entry *nodoPath, unsigned int sector);

int cargarCache(struct TDD_entry *nodoPath, char *buf, unsigned int sector);

int insertarSecOrdenado(struct t_cache **nodoCache, struct t_cache *newSector);

void sacarNodoSector(struct t_cache **nodoCache, struct t_cache *antSector);

void armarBuf(sec_arch *sectores, int tamanio, char *buf, int w, int w2, unsigned int bloque, int secPerBloque);

int leerSector(struct TDD_entry *nodoPath, unsigned int sector, char **buffer);

int escribirSector(struct TDD_entry **nodoDelPath, unsigned int sector, char  *buffer);

void crearListaDeSectores(unsigned int primerSector, int secPerBloque, unsigned int **lista);

void getBloque(struct TDD_entry *nodoPath, unsigned int sector, int bloque, int secPerBloque, sec_arch **sectores, int tamanio);

unsigned int setBloque(unsigned int sector, int bloque, int secPerBloque, char **buf, struct TDD_entry **nodoDelPath);

void writeFileCache(struct TDD_entry **nodoPath, const char *buffer, size_t size, off_t offset);

int writeFile( struct TDD_entry **nodoDelpath, char *buf, size_t size, off_t offset, const char *path);

void completarSector(unsigned int primerByteModificar, char **bufferCompletar, char *buf);

void escribirClustersRestantes(struct TDD_entry **nodoDelPath, char *buffer, unsigned int tamanioCluster, const char *path, unsigned int primerCluster, bootSector boot, int secPerBloque);

#endif /* CONEXIONES_H_ */
